package applicationPlugin;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;

import interfacesplugins.InterfaceFilesTraitement;

public class VisionneurImage extends JFrame  implements InterfaceFilesTraitement {

	private File file;
	private boolean etat;
	private JScrollPane jsp = new JScrollPane();
	private JScrollPane sp;
	private JPanel iconCadre;
	// private Icon icon;
	private JLabel l;
	private JButton b;
	JFrame fr;
	public VisionneurImage(){
		b = new JButton();
		l = new JLabel();
		iconCadre = new JPanel();
		iconCadre.setVisible(true);
		sp = new JScrollPane();
		sp.getViewport().setPreferredSize(new Dimension(400, 400));
		iconCadre.setPreferredSize(new Dimension(400, 400));
		
	}

	
	
	@Override
	public String getVersion() {
		// TODO Auto-generated method stub
		return "1.0";
	}
	 public BufferedImage resize(BufferedImage image, int width, int height) {
	    	BufferedImage resizedImage = new BufferedImage(width, height,
	    	BufferedImage.TYPE_INT_ARGB);
	    	Graphics2D g = resizedImage.createGraphics();
	    	g.drawImage(image, 0, 0, width, height, null);
	    	g.dispose();
	    	return resizedImage;
	    	}
	public JScrollPane buildIcon(File file) {
		iconCadre.removeAll();
		//System.out.println(file.getAbsolutePath());
		BufferedImage image;
		try {
			image = ImageIO.read(file);
			 BufferedImage bi = resize(image, 400, 400);
			 ImageIcon icon = new ImageIcon(bi);
				l.setPreferredSize(new Dimension(400, 400));
				l = new JLabel(icon);
				// l.setIcon( bi);
				l.setVisible(true);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		iconCadre.add(l);
		iconCadre.revalidate();
		iconCadre.setVisible(true);
		sp.getViewport().add(iconCadre);
		return sp;
	}

	
	@Override
	public String getNomPlugin() {
		// TODO Auto-generated method stub
		return "JTree ImageViewer";
	}

	@Override
	public String getDeveloppeur() {
		// TODO Auto-generated method stub
		return "EL MALLEM AMINE";
	}

	@Override
	public String[] getExtension() {
		// TODO Auto-generated method stub
		String[] exe={"jpg","png","gif"};
		return exe;
	}

	@Override
	public JFrame lancerFichier(File f) {
		// TODO Auto-generated method stub
		fr=new JFrame();
		this.file=f;
		fr.setContentPane(buildIcon(f));
		fr.setVisible(true);
		etat=true;
		return fr;
		
	}

	@Override
	public boolean getEtat() {
		// TODO Auto-generated method stub
		return etat;
	}

	@Override
	public boolean setEtat(boolean etat) {
		// TODO Auto-generated method stub
		this.etat=etat;
		return etat;
	}
}
